import Vuex from 'vuex'
import Vue from 'vue'
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		token: '',
		userinfo: {}
	},
	mutations: {
		setToken(state, token) {
			state.token = token
			uni.setStorageSync('token', token)
		},
		setuser(state, userinfo) {
			state.userinfo = userinfo
			uni.setStorageSync('userinfo', userinfo)
		}
	},
	actions: {
		INIT_STATE(context) {
			let token = uni.getStorageSync('token')
			if (token) {
				context.commit('setToken', token)
			}
		},
		INIT_USERINFO(context) {
			let userinfo = uni.getStorageSync('userinfo')
			if (userinfo) {
				context.commit('setuser', userinfo)
			}
		},
	}
})
export default store